/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaCatPanel.java
 *
 * Created on Nov 18, 2009, 4:26:19 PM
 */

package desktopapplication3;

import backend.Catalog;
import backend.Category;
import java.awt.BorderLayout;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author svillafe
 */
public class ListaCatPanel extends javax.swing.JPanel {

     private int languageCode;
     private JPanel prodConteiner;
     private Category aCateg;
     private Map<String,Category> mySubCat;

    /** Creates new form ListaCatPanel */
    
    public ListaCatPanel(Category aCateg ,JPanel productos,int languageCode) {
        this.languageCode=languageCode;
        this.aCateg= aCateg;
        this.prodConteiner=productos;
        try{
        mySubCat=Catalog.getSubcategoryList(languageCode, aCateg.getId());
        }catch(Exception a){
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(VentanaPrincipal.class);
                JOptionPane.showMessageDialog(this,resourceMap.getString("ERROR_"+a.getMessage()),"Error",1);
                System.exit(0);
        }
        initMyComponents();

    }

     private void initMyComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        SubcatList = new javax.swing.JList();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        


        SubcatList.setModel(new javax.swing.AbstractListModel() {
            Object[] strings = mySubCat.keySet().toArray();
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });

       
        SubcatList.setName("SubcatList"); // NOI18N
        jScrollPane1.setViewportView(SubcatList);

        SubcatList.addListSelectionListener(
                new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                changeProductsPane(e);
            }
        });

         ProductsPanel newOne;
         newOne = new ProductsPanel(aCateg.getId(),null,true,languageCode);
         newOne.setVisible(true);
         prodConteiner.removeAll();
         prodConteiner.setLayout(new BorderLayout());
         prodConteiner.add(newOne,BorderLayout.CENTER);
         newOne.updateUI();
         prodConteiner.updateUI();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        SubcatList = new javax.swing.JList();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        SubcatList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        SubcatList.setName("SubcatList"); // NOI18N
        jScrollPane1.setViewportView(SubcatList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    private void changeProductsPane(ListSelectionEvent e){
         ProductsPanel newOne;
         
         newOne = new ProductsPanel(aCateg.getId(),mySubCat.get(SubcatList.getSelectedValue().toString()).getId(),false,languageCode);
         newOne.setVisible(true);
         prodConteiner.removeAll();
         prodConteiner.setLayout(new BorderLayout());
         prodConteiner.add(newOne,BorderLayout.CENTER);
         newOne.updateUI();
         prodConteiner.updateUI();

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList SubcatList;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
